package org.kramaframework.event;

import java.util.List;

import org.kramaframework.exception.KramaReflectionIllegalArgumentException;
import org.kramaframework.listener.TListener;
import org.kramaframework.util.TUtil;

import com.sun.corba.se.impl.javax.rmi.CORBA.Util;

/**
 * This class content all method called by mouse event
 * @author chabane
 * @version 1.0
 */
public class TMouseEvent extends TEvent{

	/**
	 * Constructor
	 */
	public TMouseEvent(TListener mlistner){
		super(mlistner);
	}
	
	/**
	 * This method allows to build a javascript function for generate javascript
	 * code later
	 * 
	 * @param _class
	 * @param _method
	 * @param _params
	 * @param _event 
	 * @return String
	 */
	public static void show(String viewAjax, String _class, String methode, List params, String event) {
		
		try{
			// Add a function call built on widget
			mlistner.addFunctionCall(buildFunctionCall(viewAjax, _class, methode, params, "showData"));
			
			// Add type of event
			mlistner.addEvent(event);
			
			// Add a javascript dependency necessary to execute a widget
			mlistner.addJsDependency("Prototype");
			
			// Add a javascript function necessary to work
			mlistner.setJavascript(showData());
			
		}catch(KramaReflectionIllegalArgumentException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * This method allows to call Ajax request and to fill a idDiv with an
	 * object returned by Ajax Servlet
	 * 
	 * @return a fonction which allows to load data in html tag
	 */
	private static String showData() {
		return "function showData(vbName, cName, mName, paramsTab, idDiv) { \n" +

		"var url = 'result.aj?';" +
				"url +='vbName='+ vbName;\n"+
				"url += '&cName=' + cName;\n"
				+ "   url += '&mName=' + mName;\n"

				+ "  if (paramsTab.length > 0) {\n"
				+ "url += '&nParam=' + paramsTab.length;\n"

				+ "	for ( var i = 0; i < paramsTab.length; i++) {\n"
				+ "		url += '&param' + (i+1) + '=' + paramsTab[i];" + "	}\n"
				+ "} else {" + "	url += '&nParam=' + 0;" + "}\n"
				+ "new Ajax.Request(url, {\n" + "	method : 'get',\n"
				+ "	onSuccess : function(xhr) {\n"
				+ "$(idDiv).innerHTML = xhr.responseText; \n" + "},\n"
				+ "	onException : function(xhr, e) {\n"
				+ " alert(e); \n" + "}\n"
				+ "	});\n"
				+ "}\n\n";
	}
}
